﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SoSlam.Common.ClientLogic
{
    public class Signs : DrawableGameComponent
    {
        private SpriteBatch _spriteBatch;
        private TrackingCamera _camera;

        private Texture2D _signSprite;
        private Texture2D _flyingsignSprite;
        private SpriteFont _arialSpriteFont;

        // Bounce control constants
        private const float bounceHeight = 0.12f;
        private const float bounceRate = 2.0f;
        private const float bounceSync = -0.75f;
        private float bounce;

        public Signs(Game game, TrackingCamera camera)
            :base(game)
        {
            _camera = camera;
        }

        public override void Initialize()
        {
            _spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            _arialSpriteFont = Game.Content.Load<SpriteFont>("SpriteFonts/Arial");

            _signSprite = Game.Content.Load<Texture2D>("Sprites/sign2");
            _flyingsignSprite = Game.Content.Load<Texture2D>("Sprites/flying_sign");

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin();

            // Signs
            {
                var min = (float)Math.Floor(MathHelper.Clamp(_camera.Position.X, 0, _camera.Position.X) / 1000) * 1000f;
                var max = (float)Math.Ceiling((min + _camera.Fov.Width) / 1000) * 1000f;

                for (float i = min; i <= max; i += 1000)
                {
                    var position = new Vector2(i, -90) - _camera.Position;
                    _spriteBatch.Draw(_signSprite, position + new Vector2(-35, -3), Color.White);
                    _spriteBatch.DrawString(_arialSpriteFont, (i / 10).ToString(), position, Color.White);
                }
            }

            // FlyingSigns
            {
                var min = (float)Math.Floor(MathHelper.Clamp(_camera.Position.X, 0, _camera.Position.X) / 1000) * 1000f;
                var max = (float)Math.Ceiling((min + _camera.Fov.Width) / 1000) * 1000f;

                for (float i = min; i <= max; i += 1000)
                {
                    var position = new Vector2(i, -390) - _camera.Position;
                    var offset = (_camera.Position.Y < -_camera.Fov.Height) ? 40 : 390;
                    position.Y = MathHelper.Clamp(position.Y, int.MinValue, _camera.Fov.Height - offset);

                    // Bounce along a sine curve over time
                    // Include the X coordinate so that neighboring gems bounce in a nice wave pattern
                    double t = gameTime.TotalGameTime.TotalSeconds * bounceRate + (position.X + _camera.Position.X) * bounceSync;
                    bounce = (float)Math.Sin(t) * bounceHeight * _flyingsignSprite.Height;

                    position.Y += bounce;

                    _spriteBatch.Draw(_flyingsignSprite, position + new Vector2(-40, -65), Color.White);
                    _spriteBatch.DrawString(_arialSpriteFont, (i / 10).ToString(), position, Color.White);
                }
            }

            _spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
